package com.pf.controller;

import com.pf.dto.ResultResp;
import com.pf.dto.User;
import com.pf.service.CacheService;
import com.pf.utils.ResultRespUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/12/26 20:40
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    @GetMapping("/user/list")
    public ResultResp getUserList() {
        return ResultRespUtils.success(cacheService.getAllUser());
    }

    @GetMapping("/user/{userId}")
    public ResultResp getUserById(@PathVariable("userId") Integer userId) {
        return ResultRespUtils.success(cacheService.getUserById(userId));
    }

    @PostMapping("/user/add")
    public ResultResp addUserById(@RequestBody User user) {
        return ResultRespUtils.success(cacheService.addUser(user));
    }

    @GetMapping("/user/delete/{userId}")
    public ResultResp updateUserById(@PathVariable("userId") Integer userId) {
        cacheService.deleteById(userId);
        return ResultRespUtils.success();
    }

}
